package com.cosmosource.controller;

import com.alibaba.fastjson.JSONObject;
import com.cosmosource.core.utils.Base64Util;
import com.cosmosource.core.utils.RestTemplateUtil;
import com.cosmosource.model.dto.UserInfo;
import com.cosmosource.service.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cosmo
 * @version 1.0.0<br>
 */

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "OpenApi")
public class ApiController {

    @Autowired
    private DemoService demoService;

    @Value("${cosmo.platform.url.permission}")
    private String permissionUrl;

    @Value("${cosmo.platform.url.sub}")
    private String subPlatformUrl;

    @Autowired
    RestTemplateUtil restTemplateUtil;

    private static Map<String, String> headerMap = new ConcurrentHashMap<>();

    @ApiOperation(value = "获取权限")
    @GetMapping(value = "/v1/permission")
    public Object permission(HttpServletRequest request) {
        try {
            headerMap.put("Authorization", request.getHeader("Authorization"));
            JSONObject exchange = RestTemplateUtil.exchange(permissionUrl, HttpMethod.GET, headerMap);
            return exchange.get("data");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @ApiOperation(value = "测试API")
    @PostMapping(value = "/v1/demo")
    public String test() {
        return demoService.test();
    }


    @ApiOperation(value = "创建用户")
    @PostMapping(value = "/v1/user")
    public Object createUser(HttpServletRequest request) throws Exception {
        String url = subPlatformUrl + "/cosmo-mmp/v1/users";
        String token = request.getHeader("Authorization");
        UserInfo userInfo = new UserInfo();
        userInfo.setName("测试用户01");
        userInfo.setPwdFlag(0);
        userInfo.setDisable(1);
        userInfo.setExpireDate("-1");
        userInfo.setCreator(2L);
        userInfo.setUpdateBy(0L);
        userInfo.setLoginUid("test01");
        userInfo.setUserType(0);
        userInfo.setLoginPwd("Cosmo_admin1");
        userInfo.setOrgId(6L);
        userInfo.setTenantId("platform");
        userInfo.setPid(-1L);
        userInfo.setAvatar("");
        userInfo.setDateOfBirth("");
        userInfo.setRanking("");
        headerMap.put("Authorization", token);
        String jsonRequest = JSONObject.toJSONString(userInfo);
        String encodeJsonStr = Base64Util.encodeToUtf8(jsonRequest);
        return RestTemplateUtil.exchangeMMP(url, HttpMethod.POST, encodeJsonStr, MediaType.APPLICATION_JSON, headerMap);
    }

    @ApiOperation(value = "获取组织用户")
    @GetMapping(value = "/v1/tree/orgUser")
    public Object getOrgUserTree(HttpServletRequest request) throws Exception {
        String url = subPlatformUrl + "/cosmo-mmp/v1/tree/orgUser";
        String token = request.getHeader("Authorization");
        headerMap.put("Authorization", token);
        String urlWithQuery = url + "?orgId=2&hideDisable=&selfTenantIdForSuspendButton=platform&optionTenantId=platform";
        return RestTemplateUtil.exchange(urlWithQuery, HttpMethod.GET, urlWithQuery, MediaType.APPLICATION_JSON, headerMap);
    }
}
